package cn.com.note.utils.glide;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;


import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by SunFuLin on 2019/4/23.
 */

public class GlideUtils {

    /**
     * 加载资源图片
     *
     * @param imageView
     * @param drawable
     */
    public static void LoadIV_Drawable(ImageView imageView, int drawable) {
        GlideApp.with(imageView)
                .load(ContextCompat.getDrawable(imageView.getContext(), drawable))
                .centerCrop()
                .into(imageView);
    }

    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     */
    public static void LoadIV_Url(ImageView imageView, String url) {
        LoadIV_Url(imageView, url, 0);
    }

    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     * @param placeHolder
     */
    public static void LoadIV_Url(ImageView imageView, String url, int placeHolder) {
        LoadIV_Url(imageView, url, 0, 0);
    }

    /**
     * 加载网络图片
     *
     * @param imageView
     * @param url
     * @param placeHolder
     * @param error
     */
    public static void LoadIV_Url(ImageView imageView, String url, int placeHolder, int error) {
        GlideApp.with(imageView)
                .load(url)
                .placeholder(placeHolder)
                .error(error)
                //.circleCrop//转换为圆形
                .into(imageView);
    }

    /**
     * 取消加载图片
     *
     * @param imageView
     */
    public static void clearIV(ImageView imageView) {
        GlideApp.with(imageView).clear(imageView);
    }

    /**
     * 清理磁盘的缓存
     *
     * @param context
     */
    public static void clearDiskCache(Context context) {
        GlideApp.get(context.getApplicationContext()).clearDiskCache();
    }


    /**
     * 加载圆形图片
     *
     * @param iv
     * @param url
     */
    public static void LoadCircleIV(ImageView iv, String url) {
        GlideApp.with(iv)
                .load(url)
                .apply(RequestOptions.circleCropTransform())
                .into(iv);
    }

    /**
     * 加载圆形图片
     *
     * @param iv
     * @param drawable
     */
    public static void LoadCircleIV(ImageView iv, int drawable) {
        GlideApp.with(iv)
                .load(ContextCompat.getDrawable(iv.getContext(), drawable))
                .apply(RequestOptions.circleCropTransform())
                .into(iv);
    }

    /**
     * 加载图片带淡入淡出的动画效果
     *
     * @param iv
     * @param url
     */
    public static void LoadCrossFadeIV(ImageView iv, String url) {
        GlideApp.with(iv)
                .load(url)
                .transition(withCrossFade())
                .into(iv);
    }

    /**
     * 在磁盘缓存
     *
     * @param view
     * @param url
     */
    public static void CacheOnDisk(ImageView view, String url) {
        GlideApp.with(view)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    /**
     * 仅从缓存加载图片
     *
     * @param view
     * @param url
     */
    public static void loadOnlyRetrieveFromCache(ImageView view, String url) {
        GlideApp.with(view)
                .load(url)
                .onlyRetrieveFromCache(true)
                .into(view);
    }

    /**
     * 跳过内存缓存
     *
     * @param view
     * @param url
     */
    public static void loadSkipMemoryCache(ImageView view, String url) {
        GlideApp.with(view)
                .load(url)
                .skipMemoryCache(true)
                .into(view);
    }

    /**
     * 跳过磁盘缓存
     *
     * @param view
     * @param url
     */
    public static void loadCacheStrategy(ImageView view, String url) {
        GlideApp.with(view)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(view);
    }

    /**
     * 跳过所有的缓存
     *
     * @param view
     * @param url
     */
    public static void loadMemoryCache(ImageView view, String url) {
        GlideApp.with(view)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(view);
    }

    /**
     * 高斯模糊
     *
     * @param iv
     * @param url
     * @param radios
     */
    public static void loadBlurFormation(ImageView iv, String url, int radios) {
        GlideApp.with(iv)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new GlideBlurFormation(iv.getContext(), radios)))
                .into(iv);
    }
}
